package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.controller.param.ShortMessageLoginParam;
import com.example.lotterysystem.controller.param.UserPasswordLoginParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.result.UserBaseInfoResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.controller.result.UserloginResult;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import com.example.lotterysystem.service.enums.UserIdentityEnum;
import com.example.lotterysystem.service.iml.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController

public class UserController {

    @Autowired
    private UserServiceImpl service;

    private Logger log= LoggerFactory.getLogger(UserController.class);


    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        log.info("UseUserRegister  UserRegisterParam: "+param);

        UserRegisterDTO register = service.register(param);

        return CommonResult.success(convertTOUserRegister(register));

    }


    @RequestMapping("/password/login")
    public CommonResult<UserloginResult> userPasswordLogin(@RequestBody@Validated UserPasswordLoginParam param){
        log.info("userPasswordLogin  UserPasswordLoginParam: "+param);

        UserLoginDTO userLoginDTO= service.userLogin(param);

        return CommonResult.success(convertToUserLogin(userLoginDTO));

    }



    @RequestMapping("/shortMessage/login")
    public CommonResult<UserloginResult> userPasswordLogin(@RequestBody@Validated ShortMessageLoginParam param){
        log.info("userPasswordLogin  ShortMessageLoginParam: "+param);

        UserLoginDTO userLoginDTO= service.userLogin(param);

        return CommonResult.success(convertToUserLogin(userLoginDTO));


    }


    @RequestMapping("/base-user/find-list")
    public CommonResult<List<UserBaseInfoResult>> findBaseUserInfo(String identity){
        log.info("findBaseUserInfo    findBaseUserInfo:{}",identity);
        List<UserDTO> list=null;
        UserIdentityEnum userIdentityEnum = UserIdentityEnum.forName(identity);

        if(userIdentityEnum!=null)
        list=service.findUserInfo(userIdentityEnum.name());
        else list=service.findUserInfo(null);


        List<UserBaseInfoResult> ret=convertTOfindBaseUserInfo(list);
        return CommonResult.success(ret);

    }

    private List<UserBaseInfoResult> convertTOfindBaseUserInfo(List<UserDTO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Arrays.asList();
        }


        return list.stream()
                .map(userDTO->{
                    UserBaseInfoResult result=new UserBaseInfoResult();

                    result.setUserId(userDTO.getUserId());
                    result.setUserName(userDTO.getUserName());
                    result.setIdentity(userDTO.getIdentity().name());
                    return result;

                }).collect(Collectors.toList());


    }


    public UserRegisterResult convertTOUserRegister(UserRegisterDTO registerDTO){
        if(registerDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        UserRegisterResult result=new UserRegisterResult();
        result.setUserid(registerDTO.getUserId());
        return result;
    }


    private UserloginResult convertToUserLogin(UserLoginDTO userLoginDTO) {
        if(userLoginDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }

        UserloginResult result=new UserloginResult();
        result.setIdentity(userLoginDTO.getIdentity().name());
        result.setToken(userLoginDTO.getToken());
        return result;
    }
}



